package robuin.ioio.config;

import java.util.ArrayList;
import java.util.List;

import robuin.ioio.config.R;
import ioio.lib.api.AnalogInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.api.exception.OutOfResourceException;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.TextView;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;

	public class analogInput extends IOIOActivity{
		private List<TextView> listTextView_ = new ArrayList<TextView>();
	    private TextView mStatus;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent intent = getIntent();
	        String message = intent.getStringExtra(IOIOConfig.EXTRA_MESSAGE);
	        
	        setContentView(R.layout.analoginput);

	        listTextView_.add((TextView)findViewById(R.id.TextView31));
	        listTextView_.add((TextView)findViewById(R.id.TextView32));
	        listTextView_.add((TextView)findViewById(R.id.TextView33));
	        listTextView_.add((TextView)findViewById(R.id.TextView34));
	        listTextView_.add((TextView)findViewById(R.id.TextView35));
	        listTextView_.add((TextView)findViewById(R.id.TextView36));
	        listTextView_.add((TextView)findViewById(R.id.TextView37));
	        listTextView_.add((TextView)findViewById(R.id.TextView38));
	        listTextView_.add((TextView)findViewById(R.id.TextView39));
	        listTextView_.add((TextView)findViewById(R.id.TextView40));
	        listTextView_.add((TextView)findViewById(R.id.TextView41));
	        listTextView_.add((TextView)findViewById(R.id.TextView42));
	        listTextView_.add((TextView)findViewById(R.id.TextView43));
	        listTextView_.add((TextView)findViewById(R.id.TextView44));
	        listTextView_.add((TextView)findViewById(R.id.TextView45));
	        listTextView_.add((TextView)findViewById(R.id.TextView46));
	        
	        mStatus = (TextView) findViewById(R.id.status1);
	        mStatus.setText(message);
	        
	        enableUi(false);
	    }
	    
	    class IOIOThread extends BaseIOIOLooper {
	    	private List<AnalogInput> listInputs_ = new ArrayList<AnalogInput>();
			
	    	@Override
			public void setup() throws ConnectionLostException {
				try {
					// does not work for 31-46. why?
					for(int i = 31; i <= 42; i++)
						listInputs_.add(ioio_.openAnalogInput(i));
					
					enableUi(true);
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				} catch(OutOfResourceException e){
					Log.d("ADC", "Out of resource:" + e.getMessage());
				}
			}
			
			@Override
			public void loop() throws ConnectionLostException {
				try {
					boolean isVoltage = ((RadioButton)findViewById(R.id.radioVoltage)).isChecked();
					for(int i = 0; i < listInputs_.size(); i++)
					{
						if(isVoltage)
							setText(listTextView_.get(i), Float.toString(listInputs_.get(i).getVoltage()) + "V");
						else
							setText(listTextView_.get(i), Float.toString(listInputs_.get(i).read()));
						
						Thread.sleep(10);
					}
					
					Thread.sleep(100);
				} catch (InterruptedException e) {
					ioio_.disconnect();
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
			
			@Override
	    	public void disconnected(){
	    		for(int i = 0; i < listInputs_.size(); i++)
	    			listInputs_.get(i).close();
	    	}
		}
	    
	    /**
		 * A method to create our IOIO thread.
		 */
		@Override
		protected IOIOLooper createIOIOLooper() {
			return new IOIOThread();
		}
	    
		private void enableUi(final boolean enable) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					((RadioButton)findViewById(R.id.radioVoltage)).setEnabled(enable);
					((RadioButton)findViewById(R.id.radioValue)).setEnabled(enable);
				}
			});
		}
		
		private void setText(final TextView textView, final String value) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					textView.setText(value);
				}
			});
		}
	}